package com.ligongmusic.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SyncStatusObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ligongmusic.R;
import com.ligongmusic.util.DialogUtils;
import com.ligongmusic.util.GlobalParams;
import com.ligongmusic.util.JsonUtils;
import com.ligongmusic.util.LoggerUtils;
import com.ligongmusic.util.NetWorkUtils;
import com.ligongmusic.util.StringUtils;
import com.ligongmusic.util.DialogUtils.SelectCallBack;
import com.ligongmusic.util.NetWorkUtils.NetWorkCallBack;

public class UserRegister extends Activity{
	private Button regist;
	private String isSinger = "0";
	private TextView singer;
	private String code = "";
	private Button bt_get_captcha;
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what > 0)
				bt_get_captcha.setText(msg.what + "s后再次获取");
			if (msg.what == 0){
				bt_get_captcha.setText("再次获取");
				bt_get_captcha.setClickable(true);
			}
		}
	};
	private EditText et_classnum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initView();
	}

	private void getCaptchaCode(String phone){
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("phone", phone));
		code = (int)((Math.random()*9+1)*100000) + "";

		pairs.add(new BasicNameValuePair("smsCode", code));
		NetWorkUtils.getRequest(pairs, GlobalParams.GETCAPTCHA, new NetWorkCallBack() {
			@Override
			public void success(String result) {
				LoggerUtils.getLog(UserRegister.class).error(result);
				LoggerUtils.getLog(UserRegister.class).error(code);
			}
			@Override
			public void error() {
				LoggerUtils.getLog(UserRegister.class).error("error");
			}
			@Override
			public void error(String message) {
				LoggerUtils.getLog(UserRegister.class).error(message);
			}
		});
	}

	private void initView() {
		et_classnum = (EditText) findViewById(R.id.et_classnum);
		et_classnum.setVisibility("0".equals(isSinger) ? View.GONE : View.VISIBLE);
		bt_get_captcha = (Button) findViewById(R.id.bt_get_captcha);
		regist = (Button) findViewById(R.id.bt_register);
		singer = (TextView) findViewById(R.id.tv_singer);
		// 获取验证码
		bt_get_captcha.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String phone = ((EditText)findViewById(R.id.et_user_phone)).getText().toString().trim();
				if(StringUtils.isEmpty(phone)){
					Toast.makeText(getApplicationContext(), "手机号不能为空", Toast.LENGTH_SHORT).show();
					return;
				}
				if(!StringUtils.isMobile(phone)){
					Toast.makeText(getApplicationContext(), "手机号不合法", Toast.LENGTH_SHORT).show();
					return;
				}
				bt_get_captcha.setClickable(false);
				new Thread(new Runnable() {
					@Override
					public void run() {
						for (int i = 60; i >= 0; i --){
							Message.obtain(mHandler, i).sendToTarget();
							SystemClock.sleep(1000);
						}
					}
				}).start();
				getCaptchaCode(phone);
			}
		});
		regist.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String phone = ((EditText)findViewById(R.id.et_user_phone)).getText().toString().trim();
				if(StringUtils.isEmpty(phone)){
					Toast.makeText(getApplicationContext(), "手机号不能为空", Toast.LENGTH_SHORT).show();
					return;
				}
				if(!StringUtils.isMobile(phone)){
					Toast.makeText(getApplicationContext(), "手机号不合法", Toast.LENGTH_SHORT).show();
					return;
				}
				/*String username = ((EditText) findViewById(R.id.et_user_name)).getText().toString().trim();
				if(StringUtils.isEmpty(username)){
					Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_SHORT).show();
					return;
				}*/
				String musicianName = ((EditText) findViewById(R.id.et_person_name)).getText().toString().trim();
				if(StringUtils.isEmpty(musicianName)){
					Toast.makeText(getApplicationContext(), "真实姓名不能为空", Toast.LENGTH_SHORT).show();
					return;
				}
				if(!StringUtils.checkNameChese(musicianName) || musicianName.length() < 2 || musicianName.length() > 4){
					Toast.makeText(getApplicationContext(), "请输入2-4个汉字（不包含特殊符号）", Toast.LENGTH_SHORT).show();
					return;
				}
				String passWord = ((EditText)findViewById(R.id.et_pass_word)).getText().toString().trim();
				if(StringUtils.isEmpty(passWord)){
					Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
					return;
				}
				if (passWord.length() < 6 || passWord.length() > 12){
					Toast.makeText(getApplicationContext(), "请输入6-12位的密码", Toast.LENGTH_SHORT).show();
					return;
				}
				String et_captcha = ((EditText) findViewById(R.id.et_captcha)).getText().toString().trim();
				if (StringUtils.isEmpty(code) || StringUtils.isEmpty(et_captcha) || !code.equals(et_captcha)) {
					Toast.makeText(getApplicationContext(), "验证码错误", Toast.LENGTH_SHORT).show();
					return;
				}
				/*String studentId = et_classnum.getText().toString().trim();
				if ("1".equals(isSinger))
					if (StringUtils.isEmpty(studentId)){
						Toast.makeText(getApplicationContext(), "学号不能为空", Toast.LENGTH_SHORT).show();
						return;
					}*/
				register(phone,/* username,*/ passWord/*, studentId*/, musicianName);
			}
		});
		singer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogUtils.selectDialog(UserRegister.this, new String[]{"本校生", "非本校生"}, new SelectCallBack() {
					@Override
					public void select(String item) {
						isSinger = "非本校生".equals(item) ? "0" : "1";
						et_classnum.setVisibility("0".equals(isSinger) ? View.GONE : View.VISIBLE);
						singer.setText(item);
					}
				});
			}
		});
	}

	/**
	 * 注册
     */
	private void register(String phone/*, String userName*/, String passWord/*, String studentId*/, String musicianName){
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
//		pairs.add(new BasicNameValuePair("loginname", userName));
		pairs.add(new BasicNameValuePair("loginpass", passWord));
		pairs.add(new BasicNameValuePair("phone", phone));
//		pairs.add(new BasicNameValuePair("iswhutstudent", isSinger));
//		pairs.add(new BasicNameValuePair("studentid", studentId));
		pairs.add(new BasicNameValuePair("musicianname", musicianName));
		NetWorkUtils.getRequest(pairs, GlobalParams.REGISTER, new NetWorkCallBack() {
			@Override
			public void success(String result) {
				String code = JsonUtils.getString(result, "code");
				Toast.makeText(getApplicationContext(), JsonUtils.getString(result, "message"), Toast.LENGTH_SHORT).show();
				if("200".equals(code))
					// 成功
					finish();
			}
			@Override
			public void error(String message) {
				Toast.makeText(getApplicationContext(), "注册失败，请重试", Toast.LENGTH_SHORT).show();
			}
			@Override
			public void error() {
				Toast.makeText(getApplicationContext(), "注册失败，请重试", Toast.LENGTH_SHORT).show();
			}
		});
	}
}
